package kofman.test.cps;

import java.util.ArrayList;

import kofman.test.cps.R;
import kofman.test.cps.TableAdapter.TableCell;
import kofman.test.cps.TableAdapter.TableRow;
import kofman.test.info.checkStockDate;


import android.app.Activity;
import android.os.Bundle;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
/**
 * @author hellogv
 */
public class testMyListView extends Activity {
	/** Called when the activity is first created. */
	String[] title={"名稱","代號","股價","時間","其他"};
//	ArrayList<TableRow> table;
	static int width;
	public static TableCell[] titles;
	ListView lv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i("Stock", "OnCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listlayout);
		this.setTitle("ListView自适应实现表格");
		lv = (ListView) this.findViewById(R.id.ListView01);
		ArrayList<TableRow> table = new ArrayList<TableRow>();
////		TableCell[] titles = new TableCell[5];// 每行5个单元
		width = this.getWindowManager().getDefaultDisplay().getWidth()/5;
		// 定义标题
//		for (int i = 0; i < titles.length; i++) {
//			titles[i] = new TableCell(title[i], 
//									width + 8 * i,
//									LayoutParams.FILL_PARENT, 
//									TableCell.STRING);
//		}
//		table.add(new TableRow(titles));
		// 每行的数据
		checkStockDate checkStockDate=new checkStockDate();
//		Log.i("Stock", "1:"+table.size());
		table=checkStockDate.setPriceRows();
//		Log.i("Stock", "2:"+table.size());
//		for (int i = 0; i < temptable.size(); i++) {
//			table.add(temptable.get(i));
//		}
//		table.add(temptable);
//		TableCell[] cells = new TableCell[5];// 每行5个单元
//		for (int i = 0; i < cells.length - 1; i++) {
//			cells[i] = new TableCell("No." + String.valueOf(i),
//									titles[i].width, 
//									LayoutParams.FILL_PARENT, 
//									TableCell.STRING);
//		}
//		cells[cells.length - 1] = new TableCell(R.drawable.ic_launcher,
//												titles[cells.length - 1].width, 
//												LayoutParams.WRAP_CONTENT,
//												TableCell.IMAGE);
//		// 把表格的行添加到表格
////		for (int i = 0; i < 12; i++)
////			table.add(new TableRow(cells));
		TableAdapter tableAdapter = new TableAdapter(this, table);
		lv.setAdapter(tableAdapter);
		lv.setOnItemClickListener(new ItemClickEvent());
	}
	
	public ArrayList<TableRow> initTable(){
		ArrayList<TableRow> table=new ArrayList<TableAdapter.TableRow>();
		titles = new TableCell[5];// 每行5个单元
//		width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;
		for (int i = 0; i < titles.length; i++) {
			titles[i] = new TableCell(title[i], 
									width + 8 * i,
									LayoutParams.FILL_PARENT, 
									TableCell.STRING);
		}
		table.add(new TableRow(titles));
		return table;
		
	}
	public void setTitle(){
		this.setTitle("123");
		
		
	}
    //建立menu選單
    public boolean onCreateOptionsMenu(Menu menu){
    	//參數1:群組ID 參數二:itemid 參數3:ITEM順序 4:item名稱
    	menu.add(0,0,0,"重新取得資料").setIcon(R.drawable.ic_launcher);
    	menu.add(0,1,1,"離開").setIcon(R.drawable.ic_launcher);
    	menu.add(0,2,2,"版本編號").setIcon(R.drawable.ic_launcher);
    	menu.add(0,3,3,"說明").setIcon(R.drawable.ic_launcher);
    	
    	
    	return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	Log.i("Stock", "Case "+item.getItemId());
    	switch(item.getItemId()){
    		case 0:
    			Log.i("Stock", "Case 0");
//    			Toast.makeText(this, "push 0 ", Toast.LENGTH_LONG).show();
    			setTitle();
    			break;
    		case 1:
    			break;
    		case 2:
    			break;
    			
    		}
    	return super.onOptionsItemSelected(item);
    }
    
	class ItemClickEvent implements AdapterView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Toast.makeText(testMyListView.this, "选中第"+String.valueOf(arg2)+"行", 500).show();
		}
	}
	public void getAndroidVersion(){
//		android.os.Build.VERSION_CODES.;
//		Toast.makeText(this, , )
	}
}